package com.stations.controller;

import com.github.pagehelper.PageInfo;
import com.stations.common.aop.annotation.Log;
import com.stations.common.aop.enums.BusinessType;
import com.stations.common.constant.ControllerConstant;
import com.stations.common.web.base.BaseController;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.common.web.domain.response.Result;
import com.stations.common.web.domain.response.module.ResultTable;
import com.stations.domain.TTransmitterProgram;
import com.stations.domain.dtree.DTree;
import com.stations.domain.dtree.DTreeResponse;
import com.stations.domain.dtree.Status;
import com.stations.service.TrasmiterProgramService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * Describe: 发射机任务控制器
 * Author: jintao
 * CreateTime: 2019/10/23
 */
@RestController
@Api(tags = {"发射机任务管理"})
@RequestMapping(ControllerConstant.API_SYSTEM_PREFIX + "program")
public class TransmiterProgramController extends BaseController {

    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "system/program/";

    @Resource
    private TrasmiterProgramService trasmiterProgramService;




    @GetMapping(value = "/getStaionTransmitter")
    @ApiOperation(value = "获取台站发射机列表")
    @PreAuthorize("hasPermission('/system/transmittertask/getStaionTransmitter','sys:transmittertask:getStaionTransmitter')")
    public DTreeResponse getStaionProgram() {
        List<DTree> RegionList = trasmiterProgramService.getStaionTransmiterTree();
        DTreeResponse response=new DTreeResponse();
        response.setData(RegionList);
        response.setStatus(new Status());
        return response;
    }

    /**
     * 分页查询发射机节目列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/transmitterprogram/data','sys:transmitterprogram:data')")
    public ResultTable list(@ModelAttribute TTransmitterProgram tTransmitterProgram, PageDomain pageDomain) {
        PageInfo<TTransmitterProgram> pageInfo = trasmiterProgramService.selectTTransmitterTaskPage(tTransmitterProgram, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping(value = "/selectList")
    @ApiOperation(value = "发射机任务列表")
    @PreAuthorize("hasPermission('/system/transmitterprogram/selectList','sys:transmitterprogram:selectList')")
    public Result selectList() {
        List<TTransmitterProgram> lisTTransmitterTask= trasmiterProgramService.selectList();
        return Result.success(lisTTransmitterTask);
    }


    /**
     * Describe: 发射机j节目添加
     * Param ModelAndView
     * Return 发射机节目添加
     */
    @PostMapping("addProgram")
    @ApiOperation(value = "发射机j节目添加")
    @PreAuthorize("hasPermission('/system/transmitterprogram/addProgram','sys:transmitterprogram:addProgram')")
    public Result addProgram(@RequestBody TTransmitterProgram tTransmitterProgram) {
        return trasmiterProgramService.addProgram(tTransmitterProgram);
    }

    /**
     * Describe: 编辑节目
     * Param ModelAndView
     * Return 修改结果
     */
    @PostMapping("editProgram")
    @ApiOperation(value = "编辑节目")
    @PreAuthorize("hasPermission('/system/transmitterprogram/editProgram','sys:transmitterprogram:editProgram')")
    @Log(title = "编辑节目", describe = "编辑节目", type = BusinessType.EDIT)
    public Result editProgram(@RequestBody TTransmitterProgram tTransmitterProgram) {
        return trasmiterProgramService.edit(tTransmitterProgram);
    }
    /**
     * Describe: 删除节目
     * Param ModelAndView
     * Return 删除节目
     */
    @PostMapping("delProgram")
    @ApiOperation(value = "删除节目")
    @PreAuthorize("hasPermission('/system/transmitterprogram/delProgram','sys:transmitterprogram:delProgram')")
    @Log(title = "删除节目", describe = "删除节目", type = BusinessType.REMOVE)
    public Result delProgram(@RequestBody TTransmitterProgram tTransmitterProgram) {
        return trasmiterProgramService.del(tTransmitterProgram);
    }
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file) {//实现文件上传
        return trasmiterProgramService.setsFileContent(file);
    }

    @RequestMapping("/down")
    @ResponseBody
    public void down(HttpServletResponse response){//实现文件下载
        try{
            ApplicationHome applicationHome = new ApplicationHome(getClass());
            String dirPath = applicationHome.getSource().getParentFile().getParentFile().toString();

            String filename = URLEncoder.encode("运行图模板.xls", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.setHeader("resource-filename", filename);
            // 设置响应的参数
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            // 写入响应流
            ServletOutputStream outputStream = response.getOutputStream();
            File file = new File(dirPath + "\\运行图模板.xls");
                InputStream inputStream=new FileInputStream(file);
            // todo 将你要返回的文件流数据写入到outputStream中就行。
            byte[] b=new byte[1024];  //创建数据缓冲区
            int length;
            while((length=inputStream.read(b))>0){
                outputStream.write(b,0,length);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        }catch (Exception e){

        }
    }
}
